package com.fanqwz.hexieshe;

import android.databinding.DataBindingUtil;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.fanqwz.hexieshe.activity.BaseActivity;
import com.fanqwz.hexieshe.databinding.ActivityMainBinding;
import com.fanqwz.hexieshe.fragment.ArticleFragment;
import com.fanqwz.hexieshe.fragment.CategoryFragment;
import com.fanqwz.hexieshe.fragment.HeadLineFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements Navigator, FragmentManager.OnBackStackChangedListener {
    private ActivityMainBinding binding;

    @Bind(R.id.drawer_layout)
    DrawerLayout drawerLayout;

    @Bind(R.id.drawer_content)
    FrameLayout drawerContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        getSupportFragmentManager().addOnBackStackChangedListener(this);
//        android.os.Debug.waitForDebugger();

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        ButterKnife.bind(this);

        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        HeadLineFragment fragment = new HeadLineFragment();
        transaction.add(R.id.drawer_content, fragment, null);
        CategoryFragment categoryFragment = new CategoryFragment();
        categoryFragment.setDrawerLayout(drawerLayout);
        transaction.add(R.id.left_drawer, categoryFragment, null);
        transaction.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            super.onBackPressed();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public Navigator provideNavigator() {
        return this;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportActionBar().setTitle(R.string.main_page);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void showArticle(long ID) {
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        ArticleFragment articleFragment = new ArticleFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("id", ID);
        articleFragment.setArguments(bundle);
        transaction.replace(R.id.drawer_content, articleFragment);
        transaction.addToBackStack("article");
        transaction.commit();
    }

    @Override
    public void showHeadline(String category) {
        clearFragmentBackStack();
    }

    @Override
    public void showMainPage() {
        clearFragmentBackStack();
    }

    private void clearFragmentBackStack() {
        FragmentManager fragmentManager = getSupportFragmentManager();

        while (fragmentManager.getBackStackEntryCount() > 0) {
            fragmentManager.popBackStackImmediate();
        }
    }

    @Override
    public void onBackStackChanged() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        } else {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            drawerLayout.closeDrawers();
            return;
        }

        super.onBackPressed();
    }
}